package com.jyothsna.StutterWebApp.server;

import java.util.Scanner;
import com.jyothsna.StutterWebApp.client.GreetingService;
import com.jyothsna.StutterWebApp.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid.
		if (!FieldVerifier.isValidText(input)) {
			// If the input is not valid, throw an IllegalArgumentException back
			// to the client.
			throw new IllegalArgumentException(
					"Name must be at least 10 characters long");
		}
		return FindStutter(input);
	}
	private String FindStutter(String input) {
		int lineNo = 0;
		String output = "";
		String strLine = null;
		String lastone = null;
		String readLine = " <br> ";
		String[] lines = input.split("\n");
		strLine = lines[lineNo];
		lineNo++;

		while (strLine != null) {

			String[] token = strLine.split(" ");

			if (token[0].equals(lastone))// compare with the lastone
				output = output + "Repeated word on line  " + lineNo + ": "
						+ token[0] + readLine;

			for (int i = 0; i < (token.length - 1); i++) {
				//stops here if there nothing left to compare
				if (token[i].equals(token[i + 1])) { 
					output = output + "Repeated word on line " + lineNo + ": "
							+ token[i] + readLine;
				}
			}
			lastone = token[token.length - 1];
			lineNo++;
			if (lineNo <= lines.length)
				strLine = lines[lineNo - 1];
			else
				strLine = null;
		}
		return output;
	}
}